<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
    <el-form-item label="选择类型" prop="pak_type">
      <el-select v-model="ruleForm.pak_type" placeholder="请选择类型" @change="typeChange">
        <el-option label="原生zip文件" value="1"></el-option>
        <el-option label="H5zip文件" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-col :span="11">
        <el-input v-model="ruleForm.version" placeholder="请输入版本号"></el-input>
      </el-col>
      <el-col :span="11" class="checkBtn">
        <el-button type="primary" @click="checkVersion">检查版本</el-button>
      </el-col>
    </el-form-item>
    <el-form-item label="上传文件" :error="uploadError">
       <el-upload
          ref="upload"
          class="upload-demo"
          action="/management/user_upload"
          name='file_'
          :data="ruleForm"
          :auto-upload="false"
          :on-change="handleChange"
          :file-list="fileList"
          :on-success="uploadSuccessCB"
          :on-error="uploadErrorCB"
          accept="aplication/zip">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传zip文件，且不超过50MB</div>
        </el-upload>
    </el-form-item>
    <el-form-item label="升级信息" prop="version_msg">
      <el-input type="textarea" v-model="ruleForm.version_msg"></el-input>
    </el-form-item>
    <el-form-item label="下载页默认位置" prop="index_html_addr">
      <el-input v-model="ruleForm.index_html_addr"></el-input>
    </el-form-item>
    <el-form-item label-width="100px">
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { responseFormat } from '../common/dataFormat';
export default {
    data() {
      return {
        ruleForm: {
          pak_type: '',
          version: '',
          version_msg: '',
          index_html_addr: ''
        },
        uploadError: '',
        rules: {
          pak_type: [
            { required: true, message: '请选择类型', trigger: 'blur' }
          ],
          version: [
            {required: true, message: '请输入版本号', trigger: 'blur'}
          ],
          index_html_addr: [
            { required: true, message: '请填写默认下载位置', trigger: 'change' }
          ]
        },
        fileList: []
      };
    },
    methods: {
      typeChange(value) {
        if (value === '1') {
          this.ruleForm.index_html_addr = 'static/htapp/app/index.html';
        } else {
          this.ruleForm.index_html_addr = 'static/dist/';
        }
      },
      checkVersion() {
        this.$refs.ruleForm.validateField('version', err => {
          return false;
        });
        this.$refs.ruleForm.validateField('pak_type', err => {
          return false;
        });
        this.$http.get('/api/PROJECT0/check_version', {
          params:{
            version: this.ruleForm.version,
            type: this.ruleForm.pak_type
          }
        }).then(res => {
          const result = responseFormat(res.data.table)[0];
          console.log(result);
          if (result.v_type === 1) {
            this.$message({
              message: '版本重复',
              type: 'error'
            })
          } else {
            this.$message({
              message: '版本可用',
              type: 'success'
            })
          }
        }, err => {
          console.log(err);
        }).catch(err => {
          console.log(err);
        })
      },
      handleChange(file, fileList) {
        var str = file.name || '';
        if (!str.toLowerCase().match(/\.zip/)) {
         this.uploadError = '只能上传zip文件';
         this.fileList = [];
        } else {
          this.uploadError = '';
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$refs.upload.submit();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      uploadSuccessCB() {
        this.$message({
          message: '上传成功',
          type: 'success'
        })
      },
      uploadErrorCB(err) {
        this.$message({
          message: err,
          type: 'error'
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
  .checkBtn {
    margin-left: 10px;
  }
</style>
